<template>
  <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item label="编码" prop="code">
      <el-input v-model="form.code" />
    </el-form-item>
    <el-form-item label="规格" prop="specification">
      <el-input v-model="form.specification" />
    </el-form-item>
    <el-form-item label="单位" prop="unit">
      <el-input v-model="form.unit" />
    </el-form-item>
    <el-form-item label="禁用" prop="enable">
      <el-checkbox v-model="form.enable" />
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="form.remark" />
    </el-form-item>
    <el-form-item label="经济年限" prop="economicYears">
      <el-input v-model="form.economicYears" />
    </el-form-item>
    <el-form-item label="残值率" prop="residualRate">
      <el-input v-model="form.residualRate" />
    </el-form-item>
    <div class="com-form-title">附件</div>
    <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
    <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
  </el-form>
</template>

<script>
import { sexTypes, bloodTypes } from '@/utils/dicData'
export default {
  name: 'AddMaterial',
  props: {
    data: {
      type: Object,
      default() {
        return {
          fileList: []
        }
      }
    },
    type: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      sexTypes,
      bloodTypes,
      form: {
        'code': '',
        'economicYears': '',
        'enable': '',
        'name': '',
        // 'parentId': '',
        'remark': '',
        'residualRate': '',
        'specification': '',
        'unit': ''
      },
      rules: {
        name: [
          { required: true, message: '请输入机械名称', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
        this.fileList = []
      }
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        Object.keys(this.form).forEach(key => {
          const val = newData[key]
          this.form[key] = val
        })
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    validateForm(fn) {
      this.$refs.form.validate((valid) => {
        fn(valid)
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
